home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / c01lab1.zip / SIMLATRS / AUTO.BDY < prev    next >
Text File  |  1992-11-11  |  6KB  |  160 lines

  1. -- *******************************************
  2. -- *                                         *
  3. -- *  Automobile_Interface                   *  BODY
  4. -- *                                         *
  5. -- *******************************************
  6. package body Automobile_Interface is
  7.  
  8.   type AUTO_STATE is (STOPPED, DECELERATING, ACCELERATING, HOLDING);
  9.  
  10.   subtype ACCELERATION is FLOAT;
  11.  
  12.   Engine_Is_On      : BOOLEAN := FALSE;
  13.   Brake_Is_On       : BOOLEAN := FALSE;
  14.   Current_State     : AUTO_STATE := STOPPED;
  15.  
  16.   Current_Speed     : SPEED := 0.0;         -- MPH
  17.   Starting_Speed    : SPEED := 0.0;         -- MPH
  18.   Accel_Rate        : constant ACCELERATION := 2.0;  -- MPH/S
  19.   Decel_Rate        : constant ACCELERATION := 1.0;  -- MPH/S
  20.   Braking_Rate      : constant ACCELERATION := 3.0;  -- MPH/S
  21.   Elapsed_Time      : NATURAL := 0;         -- Seconds
  22.  
  23.   -- ...........................................
  24.   -- .                                         .
  25.   -- .  Automobile_Interface.Turn_On_Engine    .  BODY
  26.   -- .                                         .
  27.   -- ...........................................
  28.   procedure Turn_On_Engine is
  29.   begin
  30.     Engine_Is_On := TRUE;
  31.   end Turn_On_Engine;
  32.  
  33.   -- ...........................................
  34.   -- .                                         .
  35.   -- .  Automobile_Interface.Turn_Off_Engine   .  BODY
  36.   -- .                                         .
  37.   -- ...........................................
  38.   procedure Turn_Off_Engine is
  39.   begin
  40.     Engine_Is_On      := FALSE;
  41.     Brake_Is_On       := FALSE;
  42.     Current_State     := STOPPED;
  43.     Current_Speed     := 0.0;
  44.     Starting_Speed    := 0.0;
  45.   end Turn_Off_Engine;
  46.  
  47.   -- ....................................................
  48.   -- .                                                  .
  49.   -- .  Automobile_Interface.Depress_Accelerator_Pedal  .  BODY
  50.   -- .                                                  .
  51.   -- ....................................................
  52.   procedure Depress_Accelerator_Pedal is
  53.   begin
  54.     Elapsed_Time   := 0;
  55.     Current_State  := ACCELERATING;
  56.     Starting_Speed := Current_Speed;
  57.     Brake_Is_On    := FALSE;
  58.   end Depress_Accelerator_Pedal;
  59.  
  60.   -- ....................................................
  61.   -- .                                                  .
  62.   -- .  Automobile_Interface.Hold_Accelerator_Pedal     .  BODY
  63.   -- .                                                  .
  64.   -- ....................................................
  65.   procedure Hold_Accelerator_Pedal is
  66.   begin
  67.     Current_State     := HOLDING;
  68.     Brake_Is_On       := FALSE;
  69.   end Hold_Accelerator_Pedal;
  70.  
  71.   -- ....................................................
  72.   -- .                                                  .
  73.   -- .  Automobile_Interface.Release_Accelerator_Pedal  .  BODY
  74.   -- .                                                  .
  75.   -- ....................................................
  76.   procedure Release_Accelerator_Pedal is
  77.   begin
  78.     Elapsed_Time      := 0;
  79.     Current_State     := DECELERATING;
  80.     Starting_Speed    := Current_Speed;
  81.     Brake_Is_On       := FALSE;
  82.   end Release_Accelerator_Pedal;
  83.  
  84.   -- ..............................................
  85.   -- .                                            .
  86.   -- .  Automobile_Interface.Depress_Brake_Pedal  .  BODY
  87.   -- .                                            .
  88.   -- ..............................................
  89.   procedure Depress_Brake_Pedal is
  90.   begin
  91.     Elapsed_Time      := 0;
  92.     Current_State     := DECELERATING;
  93.     Starting_Speed    := Current_Speed;
  94.     Brake_Is_On       := TRUE;
  95.   end Depress_Brake_Pedal;
  96.  
  97.   -- ..............................................
  98.   -- .                                            .
  99.   -- .  Automobile_Interface.Release_Brake_Pedal  .  BODY
  100.   -- .                                            .
  101.   -- ..............................................
  102.   procedure Release_Brake_Pedal is
  103.   begin
  104.     Current_State := DECELERATING;
  105.     Brake_Is_On       := FALSE;
  106.   end Release_Brake_Pedal;
  107.  
  108.   -- ...........................................
  109.   -- .                                         .
  110.   -- .  Automobile_Interface.Sensed_Speed      .  BODY
  111.   -- .                                         .
  112.   -- ...........................................
  113.   function Sensed_Speed return SPEED is
  114.   begin
  115.     return Current_Speed;
  116.   end Sensed_Speed;
  117.  
  118.   -- ...........................................
  119.   -- .                                         .
  120.   -- .  Automobile_Interface.Update            .  BODY
  121.   -- .                                         .
  122.   -- ...........................................
  123.   procedure Update is
  124.   begin
  125.     if Engine_Is_On then
  126.       Elapsed_Time := Elapsed_Time + 1;
  127.       case Current_State is
  128.         when ACCELERATING =>
  129.           begin
  130.             Current_Speed := Starting_Speed +
  131.                              SPEED(Accel_Rate * FLOAT(Elapsed_Time));
  132.           exception
  133.             when others =>
  134.               Current_Speed := Maximum_Speed;
  135.               Current_State := HOLDING;
  136.           end;
  137.         when DECELERATING =>
  138.           begin
  139.             if Brake_Is_On then
  140.               Current_Speed := Starting_Speed -
  141.                                SPEED(Braking_Rate * FLOAT(Elapsed_Time));
  142.             else
  143.               Current_Speed := Starting_Speed -
  144.                                SPEED(Decel_Rate * FLOAT(Elapsed_Time));
  145.             end if;
  146.           exception
  147.             when others =>
  148.               Current_Speed := 0.0;
  149.               Current_State := STOPPED;
  150.           end;
  151.         when HOLDING | STOPPED =>
  152.           null;
  153.       end case;
  154.     end if;
  155.   end Update;
  156.  
  157. begin -- Initialization
  158.   Turn_Off_Engine;
  159. end Automobile_Interface;
  160.